class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
}

/**
 * 二叉树路径总和
 */
public class Solution {
    private int sum = 0;
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) {
            return false;
        }
        if(root.left == null && root.right == null) {
            sum += root.val;
            if(sum == targetSum) {
                return true;
            }else {
                sum -= root.val;
                return false;
            }
        }

        sum += root.val;
        //如果代码到该位置说明还没到叶子节点，但是sum已经大于targetSum，直接返回
        //数值可能是负数，这个判断条件不能要
        /* if(sum >= targetSum) {
            sum -= root.val;
            return false;
        } */
        boolean flgLeft = hasPathSum(root.left, targetSum);
        if(flgLeft == true) {
            return true;
        }
        boolean flgRight = hasPathSum(root.right, targetSum);
        if(flgRight == true) {
            return true;
        }
        sum -= root.val;
        return false;
    }
}